package com.ciwong.epaper.modules.cordva.dao;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.ciwong.epaper.modules.cordva.bean.AddErrbookResponce;
import com.ciwong.epaper.modules.cordva.bean.ErrorBook;
import com.ciwong.epaper.modules.cordva.bean.VideoPlayInfo;
import com.ciwong.libs.utils.volley.BaseRequest;
import com.ciwong.epaper.util.HttpRequest;
import com.ciwong.mobilelib.i.BaseCallBack;
import com.ciwong.mobilelib.utils.BaseRequestUtil;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by young on 2016/7/22.
 * 请求类
 */
public class CordvaRequest {

    /**
     * 增加错题本
     *
     * @param brandId
     * @param doWorkId
     * @param typeId
     * @param packageId
     * @param cId
     * @param moduleId
     *  @param packageId
     * @param versionId
     * @param subjectId
     * @param callBack
     */
    public static void addErrorBook(String brandId,String doWorkId,String typeId,String packageId, String cId, String moduleId,String versionId,String resourceName,String subjectId,
                                    final BaseCallBack callBack)
    {
        Map<String, String> params = new HashMap<String, String>();
        params.put("brandId",brandId);
        params.put("doWorkId",doWorkId);
        params.put("typeId",typeId);
        params.put("packageId",packageId);
        params.put("cId", cId);
        params.put("moduleId", moduleId);
        params.put("versionId",versionId);
        params.put("resourceName", URLEncoder.encode(resourceName));
        params.put("subjectId",subjectId);
        params.put(BaseRequest.REQUEST_ACTION, CordvaAction.ADD_BOOK_ERROR);
        params.put(BaseRequest.REQUEST_MODULES, CordvaAction.class.getName());
        HttpRequest request = new HttpRequest(params,
                new Response.ErrorListener()
                {
                    @Override
                    public void onErrorResponse(VolleyError volleyError)
                    {
                        callBack.failed(volleyError);
                    }
                }, new BaseRequest.ResponseCallback()
        {
            @Override
            public void error(int errorCode, String msg)
            {
                callBack.failed(errorCode, msg);
            }

            @Override
            public void success(Object obj, int resultCode,
                                int errorCode, String msg)
            {
                if (resultCode == 0 && errorCode == 0)
                {
                    callBack.success(obj);
                }
                else
                {
                    callBack.failed(errorCode, msg);
                }
            }
        });

        request.setResponseDataType(HttpRequest.RESPONSE_DATA_TYPE_OBJECT);
        request.setResponseClazz(AddErrbookResponce.class);
        BaseRequestUtil.getInstance().add(request);
    }


    /**
     * 查询错题列表
     *
     * @param brandId
     * @param pIndex
     * @param psize
     * @param callBack
     */
    public static void selectErrorBook(String brandId,String pIndex,String psize,String subjectId,
                                    final BaseCallBack callBack)
    {

        Map<String, String> params = new HashMap<String, String>();
        params.put("brandId",brandId);
        params.put("pIndex",pIndex);
        params.put("psize",psize);
        params.put("subjectId",subjectId);
        params.put(BaseRequest.REQUEST_ACTION, CordvaAction.QUERY_ERROR_BOOK);
        params.put(BaseRequest.REQUEST_MODULES, CordvaAction.class.getName());
        HttpRequest request = new HttpRequest(params,
                new Response.ErrorListener()
                {
                    @Override
                    public void onErrorResponse(VolleyError volleyError)
                    {
                        callBack.failed(volleyError);
                    }
                }, new BaseRequest.ResponseCallback()
        {
            @Override
            public void error(int errorCode, String msg)
            {
                callBack.failed(errorCode, msg);
            }

            @Override
            public void success(Object obj, int resultCode,
                                int errorCode, String msg)
            {
                if (resultCode == 0 && errorCode == 0)
                {
                    callBack.success(obj);
                }
                else
                {
                    callBack.failed(errorCode, msg);
                }
            }
        });


        request.setResponseDataType(HttpRequest.RESPONSE_DATA_TYPE_OBJECT);
        request.setResponseClazz(ErrorBook.class);
        BaseRequestUtil.getInstance().add(request);
    }



    /**
     * 删除错题
     *
     * @param brandId
     * @param versionId
     * @param
     * @param callBack
     */
    public static void delectErrorBook(String brandId,String versionId,
                                    final BaseCallBack callBack)
    {
        //String key = "0d61cdfebcc4ac902ffce5c6076e2f914d";
        Map<String, String> params = new HashMap<String, String>();
        params.put("brandId",brandId);
        params.put("versionId",versionId);
        params.put(BaseRequest.REQUEST_ACTION, CordvaAction.DELETE_ERROR_BOOK);
        params.put(BaseRequest.REQUEST_MODULES, CordvaAction.class.getName());
        HttpRequest request = new HttpRequest(params,
                new Response.ErrorListener()
                {
                    @Override
                    public void onErrorResponse(VolleyError volleyError)
                    {
                        callBack.failed(volleyError);
                    }
                }, new BaseRequest.ResponseCallback()
        {
            @Override
            public void error(int errorCode, String msg)
            {
                callBack.failed(errorCode, msg);
            }

            @Override
            public void success(Object obj, int resultCode,
                                int errorCode, String msg)
            {
                if (resultCode == 0 && errorCode == 0)
                {
                    callBack.success(obj);
                }
                else
                {
                    callBack.failed(errorCode, msg);
                }
            }
        });

        request.setResponseDataType(HttpRequest.RESPONSE_DATA_TYPE_OBJECT);
        request.setResponseClazz(Boolean.class);
        BaseRequestUtil.getInstance().add(request);
    }


    /**
     * [APP]根据作业ID获取作业详情
     * @param workId
     * @param callBack
     */
    public static void getUserWorkDetail(String workId,
                                       final BaseCallBack callBack)
    {
        Map<String, String> params = new HashMap<String, String>();
        params.put("workId",workId);
        params.put(BaseRequest.REQUEST_MODULES, CordvaAction.class.getName());
        params.put(BaseRequest.REQUEST_ACTION, CordvaAction.GET_USER_WORKDETAIL);
        HttpRequest request = new HttpRequest(params,
                new Response.ErrorListener()
                {
                    @Override
                    public void onErrorResponse(VolleyError volleyError)
                    {
                        callBack.failed(volleyError);
                    }
                }, new BaseRequest.ResponseCallback()
        {
            @Override
            public void error(int errorCode, String msg)
            {
                callBack.failed(errorCode, msg);
            }

            @Override
            public void success(Object obj, int resultCode,
                                int errorCode, String msg)
            {
                if (resultCode == 0 && errorCode == 0)
                {
                    callBack.success(obj);
                }
                else
                {
                    callBack.failed(errorCode, msg);
                }
            }
        });

        request.setResponseDataType(HttpRequest.RESPONSE_DATA_TYPE_OBJECT);
        request.setResponseClazz(VideoPlayInfo.class);
        BaseRequestUtil.getInstance().add(request);
    }





    /**
     *
     * @param brandId
     * @param serviceId 服务ID
     * @param productId 书籍ID
     * @param quesId 题目ID
     * @param letv  乐视 id
     * @param callBack
     */
    public static void getVideoPlayInfo(int brandId,int serviceId, String productId,String quesId,String letv,
                                         final BaseCallBack callBack)
    {
        Map<String, String> params = new HashMap<String, String>();
        params.put("brandId",brandId+"");
        params.put("serviceId",serviceId+"");
        params.put("productId",productId);
        params.put("quesId",quesId);
        params.put("letv",letv);

        params.put(BaseRequest.REQUEST_ACTION, CordvaAction.GET_VIDEO_PLAY_INFO);

        params.put(BaseRequest.REQUEST_MODULES, CordvaAction.class.getName());
        HttpRequest request = new HttpRequest(params,
                new Response.ErrorListener()
                {
                    @Override
                    public void onErrorResponse(VolleyError volleyError)
                    {
                        callBack.failed(volleyError);
                    }
                }, new BaseRequest.ResponseCallback()
        {
            @Override
            public void error(int errorCode, String msg)
            {
                callBack.failed(errorCode, msg);
            }

            @Override
            public void success(Object obj, int resultCode,
                                int errorCode, String msg)
            {
                if (resultCode == 0 && errorCode == 0)
                {
                    callBack.success(obj);
                }
                else
                {
                    callBack.failed(errorCode, msg);
                }
            }
        });

        request.setResponseDataType(HttpRequest.RESPONSE_DATA_TYPE_OBJECT);
        request.setResponseClazz(VideoPlayInfo.class);
        BaseRequestUtil.getInstance().add(request);
    }

}
